#include "client.h"

client::client(int port,string ip):server_port(port),server_ip(ip){}

client::~client()
{
	close(sock);
}

void client::run()
{
	sock=socket(AF_INET,SOCK_STREAM,0);

	struct sockaddr_in servaddr;
	memset(&servaddr,0,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	servaddr.sin_port=htons(server_port);
	servaddr.sin_addr.s_addr=inet_addr(server_ip.c_str());

	if(connect(sock,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
	{
		perror("connect");
		exit(1);
	}
	cout<<"连接服务器成功\n";

	HandleClient(sock);
	return;
}

void client::HandleClient(int conn){
    int choice;
    bool if_login=false;//记录是否登录成功
    string login_name;
    user user1,user2;
    string pass;

    cout<<" ------------------\n";
    cout<<"|                  |\n";
    cout<<"| 请输入你要的选项:|\n";
    cout<<"|    0:退出        |\n";
    cout<<"|    1:登录        |\n";
    cout<<"|    2:注册        |\n";
    cout<<"|                  |\n";
    cout<<" ------------------ \n\n";
    cout<<"输入数字选择:";
    //开始处理各种事务
    while(1){
        if(if_login)
            break;
        cin>>choice;
        if(choice==0)
            break;
        //注册
        else if(choice==2)
	{
            cout<<"注册的用户名:";
            user1.target=0;
            cin>>user1.name;
            while(1)
	    {
                cout<<"密码:";
                cin>>user1.pass;
                cout<<"确认密码:";
                cin>>pass;
                cout<<"角色:";
                cin>>user1.role;
                if(user1.pass==pass)
                    break;
                else
                    cout<<"两次密码不一致!\n\n";
            }
	    send(conn,(char*)&user1,sizeof(user1),0);
	    memset(&user2,0,sizeof(user2));
	    recv(sock,(char*)&user2,sizeof(user2),0);//接收响应
	    if(user2.result==1)
	    {
	          cout<<"注册失败\n\n";
	    }
            else{
                  cout<<"注册成功\n\n";
            }
            cout<<"输入数字选择:";
        }
        //登录
	else if(choice==1&&!if_login)
	{
            while(1){
                cout<<"用户名:";
                cin>>user1.name;
                cout<<"密码:";
                cin>>user1.pass;
                user1.target=1;
                send(sock,(char*)&user1,sizeof(user1),0);//发送登录信息
                char buffer[1000];
                memset(buffer,0,sizeof(buffer));
                recv(sock,buffer,sizeof(buffer),0);//接收响应
                string recv_str(buffer);
                //登录成功
                if(recv_str.substr(0,2)=="ok"){
                    if_login=true;
                    login_name=user1.name;
                    cout<<"登录成功\n\n";
                    break;
                }
                //登录失败
                else
                    cout<<"密码或用户名错误！\n\n";
            }
        }
    }
    //登录成功
    if(if_login==true){
    system("clear");//清空终端d
    while(1){
        cout<<"        欢迎回来,"<<login_name<<endl;
        cout<<" -------------------------------------------\n";
        cout<<"|                                           |\n";
        cout<<"|          请选择你要的选项：               |\n";
        cout<<"|              0:退出                       |\n";
        cout<<"|              1:记录                       |\n";
        cout<<"|              2:查看                       |\n";
	cout<<"|              3:新建项目                   |\n";
        cout<<"|              4:审核项目                   |\n";
	cout<<"|              5:提交                       |\n";
	cout<<"|              6:清空终端                   |\n";
        cout<<"|                                           |\n";
        cout<<" ------------------------------------------- \n\n";
	int i;
	cout<<"输入数字选择:";
	cin>>i;
	cout<<i<<endl;
	switch(i){
	case 1:
	        record(user1,user2);
		break;
	case 2:
	        view(user1,user2);
		break;
	case 3:
	        cout<<"输入项目名称:";
	        cin>>user1.itemname;
	        cout<<"输入金额";
	        cin>>user1.count;
	        cout<<"输入负责人名字";
	        cin>>user1.name;
	        user1.target=4;
	        send(sock,(char*)&user1,sizeof(user1),0);
	        memset(&user2,0,sizeof(user2));
	        recv(sock,(char*)&user2,sizeof(user2),0);//接收响应
	        if(user2.result==1)
	        {
	          cout<<"新建失败\n\n";
	        }
                else{
                  cout<<"新建成功\n\n";
                }
                cout<<"输入数字选择:";
		break;
	case 4:
	        cout<<"输入项目名称:";
	        cin>>user1.itemname;
	        user1.target=7;
	        send(sock,(char*)&user1,sizeof(user1),0);
	        memset(&user2,0,sizeof(user2));
	        recv(sock,(char*)&user2,sizeof(user2),0);//接收响应
	        if(user2.result==1)
	        {
	            cout<<"该项目不存在"<<endl;
	        }
	        else{
	        cout<<"项目id:"<<user2.itemid<<"项目名称:"<<user2.itemname<<"负责人名字:"<<user2.name<<"金额:"<<user2.count<<"审核状态:"<<user2.audit<<endl;
	        cout<<"0代表未审核，1代表审核通过，2代表关闭项目"<<endl;
	        }
		break;
	case 5:
	        
		break;
	case 6:
	        system("clear");//清空终端d
	        break;
	case 0:
	        exit(1);
		break;
	default:
		break;
	}
    }
    }
    
}

void client::view(user user1,user user2)
{
      while(1)
      {
        int id;
        cout<<" -------------------------------------------\n";
        cout<<"|                                            |\n";
        cout<<"|          请选择你要的选项：                 |\n";
        cout<<"|              0:退出                        |\n";
        cout<<"|              1:查看特定项目                |\n";
        cout<<"|              2:查看全部项目                |\n";
        cout<<"|              3:清空终端                    |\n";
        cout<<"|                                           |\n";
        cout<<" ------------------------------------------- \n\n";
        int i;
	cout<<"输入数字选择:";
	cin>>i;
        switch(i){
	case 1:
	        user1.target=5;
	        cout<<"输入项目项目名称:";
	        cin>>user1.itemname;
	        send(sock,(char*)&user1,sizeof(user1),0);
                memset(&user2,0,sizeof(user2));
	        recv(sock,(char*)&user2,sizeof(user2),0);//接收响应
	        if(user2.result==1)
	        {
	            break;
	        }
	        cout<<"项目id:"<<user2.itemid<<"项目名称:"<<user2.itemname<<"负责人名字:"<<user2.name<<"金额:"<<user2.count<<endl;
		break;
	case 2:
	        user1.target=6;
	        send(sock,(char*)&user1,sizeof(user1),0);
	        while(1)
	        {
	        memset(&user2,0,sizeof(user2));
	        recv(sock,(char*)&user2,sizeof(user2),0);//接收响应
	        if(id==user2.itemid)
	        {
	               break;
	        }
                cout<<"项目id:"<<user2.itemid<<"项目名称:"<<user2.itemname<<"负责人名字:"<<user2.name<<"金额:"<<user2.count<<endl;
                id=user2.itemid;
                }
		break;
	case 3:
	        system("clear");//清空终端d
	        break;
        case 0:
                return;
                break;
	default:
		break;
	}
	}
}

void client::record(user user1,user user2)
{
      while(1)
      {
        system("clear");//清空终端d
        cout<<" -------------------------------------------\n";
        cout<<"|                                           |\n";
        cout<<"|          请选择你要的选项：                |\n";
        cout<<"|              0:退出                       |\n";
        cout<<"|              1:记录支出                   |\n";
        cout<<"|              2:记录收入                   |\n";
        cout<<"|                                           |\n";
        cout<<" ------------------------------------------- \n\n";
        int i;
	cout<<"输入数字选择:";
	cin>>i;
        switch(i){
	case 1:
	        user1.target=2;
	        cout<<"输入项目名称:";
	        cin>>user1.itemname;
	        cout<<"记录支出:";
	        cin>>user1.count;
	        send(sock,(char*)&user1,sizeof(user1),0);
                memset(&user2,0,sizeof(user2));
	        recv(sock,(char*)&user2,sizeof(user2),0);//接收响应
	        if(user2.result==1)
	        {
	            cout<<"记录支出失败"<<endl;
	            break;
	        }
	        else
	        {
	            cout<<"记录成功"<<endl;
	        }
		break;
	case 2:
	        user1.target=3;
	        cout<<"输入项目名称:";
	        cin>>user1.itemname;
	        cout<<"记录收入:";
	        cin>>user1.count;
	        send(sock,(char*)&user1,sizeof(user1),0);
                memset(&user2,0,sizeof(user2));
	        recv(sock,(char*)&user2,sizeof(user2),0);//接收响应
	        if(user2.result==1)
	        {
	            cout<<"记录支出失败"<<endl;
	            break;
	        }
	        else
	        {
	            cout<<"记录成功"<<endl;
	        }
		break;
        case 0:
                return;
                break;
	default:
		break;
	}
	}
}

void client::SendMsg(int conn){
	char sendbuf[150];
	while(1)
	{
		memset(sendbuf,0,sizeof(sendbuf));
		cout<<"退出请输入exit"<<endl;
		cout<<"输入发送信息:"<<endl;
		cin.getline(sendbuf,100);
		if(strlen(sendbuf)>100)
		{
			cout<<"输入字符应当小于或等于100"<<endl;
			continue;
		}
		int ret=send(conn,sendbuf,strlen(sendbuf),0);
                cout<<"发送给服务器的信息："<<sendbuf<<endl;
		cin.clear();
		cin.sync();
		if(strcmp(sendbuf,"exit")==0||ret<=0)
			break;
	}
}

void client::RecvMsg(int conn)
{
	char buffer[1000];
	while(1)
	{
		memset(buffer,0,sizeof(buffer));
		int len=recv(conn,buffer,sizeof(buffer),0);
		if(len<=0)
			break;
		printf("收到服务器发来的信息：%s\n",buffer);
	}
}
